//
//  SESettingListCell.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/15.
//

import UIKit

enum SESettingListCellType: Int {
    case userId = 0
    case avator
    case identity
    case password
    case update

    var titleText: String {
        switch self {
        case .userId: return "我的ID"
        case .avator: return "头像"
        case .identity: return "昵称"
        case .password: return "修改密码"
        case .update: return "检查更新"
        }
    }
}

protocol SESettingListCellDelegate: NSObjectProtocol {
    func settingListCellEndEditing(listCell: SESettingListCell)
}

class SESettingListCell: UITableViewCell {

    static let className = "SESettingListCell"
    static let reuseId = className

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailLabel: UILabel!
    @IBOutlet weak var avatorImgView: UIImageView! {
        didSet {
            avatorImgView.layer.cornerRadius = 4
            avatorImgView.layer.masksToBounds = true
        }
    }

    @IBOutlet weak var arrowImgView: UIImageView!

    @IBOutlet weak var textField: UITextField! {
        didSet {
            textField.delegate = self
        }
    }

    weak var delegate: SESettingListCellDelegate?

    var type: SESettingListCellType = .avator {
        didSet {
            titleLabel.text = type.titleText
            textField.isHidden = true
            arrowImgView.isHidden = false
            avatorImgView.isHidden = true
            switch type {
            case .userId:
                arrowImgView.isHidden = true
                avatorImgView.isHidden = true
                detailLabel.isHidden = false
                detailLabel.text = UserDefaults.userId
            case .avator:
                avatorImgView.isHidden = false
                detailLabel.isHidden = true
                avatorImgView.kf.setAvatarImage(urlString: UserDefaults.userAvatar)
            case .identity:
                detailLabel.isHidden = true
                textField.isHidden = false
                textField.text = UserDefaults.userNick
                textField.returnKeyType = .done
                textField.addRightButtonOnKeyboardWithText("完成", target: self, action: #selector(rightButtonAction))
            case .password:
                detailLabel.isHidden = true
            case .update:
                detailLabel.isHidden = true
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func updateCell(indexPath: IndexPath) {
        type = SESettingListCellType(rawValue: indexPath.row) ?? .avator
    }

    @objc private func rightButtonAction() {
        delegate?.settingListCellEndEditing(listCell: self)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

extension SESettingListCell: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        delegate?.settingListCellEndEditing(listCell: self)
        return true
    }

}
